package com.example.backend.controller;

import com.example.backend.common.api.R;
import com.example.backend.common.api.ResultCode;
import com.example.backend.service.CaptchaService;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码控制器
 */
@Validated
@RestController
@RequestMapping("/captcha")
@RequiredArgsConstructor
public class CaptchaController {
    
    private final CaptchaService captchaService;
    
    /**
     * 发送验证码
     */
    @PostMapping("/send")
    public R<Void> sendCaptcha(
            @RequestParam @NotBlank(message = "邮箱不能为空") 
            @Email(message = "邮箱格式不正确") String email,
            @RequestParam @NotBlank(message = "验证码类型不能为空") String type) {
        // 验证类型是否合法
        if (!isValidType(type)) {
            return R.fail(ResultCode.INVALID_CAPTCHA_TYPE);
        }
        captchaService.sendCaptcha(email, type);
        return R.ok();
    }
    
    /**
     * 验证类型是否合法
     */
    private boolean isValidType(String type) {
        return "register".equals(type) || 
               "login".equals(type) || 
               "reset".equals(type);
    }
} 